package edu.usx.server.controller;


import edu.usx.server.config.MailService;
import edu.usx.server.pojo.RespBean;

import edu.usx.server.pojo.dto.*;
import edu.usx.server.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author RawMieEgg
 */
@RestController
@RequestMapping("/user")
@Api(value = "UserController")
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private MailService mailService;

    /**
     * 用户信息修改
     * @param userDto
     * @return
     */
    @PutMapping
    @ApiOperation("用户信息修改")
    public RespBean userInfoUpdate(@RequestBody UserDto userDto){
        log.info("修改用户信息：{}",userDto);
        //头像上传
        return  userService.updateById(userDto);
    }

    /**
     * 文件上传
     * @return
     */
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public RespBean upload(@RequestParam("file") MultipartFile file){
        log.info("文件上传：{}",file);
        return userService.uploadImage(file);
    }

    /**
     * 用户注册
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public RespBean register(@RequestBody UserregisterDto user, HttpServletRequest request){
        return userService.register(user, request);
    }

    @ApiOperation(value="注册发送邮箱验证码")
    @PostMapping("/registersendEmailCode")
    public RespBean registersendEmailCode(@RequestParam String email, HttpServletRequest request){

        return mailService.sendEmailCode(email,request);
    }

    @ApiOperation(value="用户重置密码发送邮箱验证码")
    @PostMapping("/sendEmailCode")
    public RespBean sendEmailCode(@RequestParam String email, HttpServletRequest request){
//        if (userService.getUserByUserEmail(email)==null){
//            return RespBean.error("邮箱不存在!");
//        }
        return mailService.sendEmailCode(email,request);
    }

    @ApiOperation(value="重置密码")
    @PutMapping("/resetPasswordByEmail")
    public RespBean resetPasswordByEmail(@RequestBody UserResetPwdDto userResetPwdDto, HttpServletRequest request){
        return userService.resetPasswordByEmail(userResetPwdDto,request);
    }

    @ApiOperation(value="修改密码")
    @PutMapping("/updatePassword")
    public RespBean updatePassword(@RequestBody UpdatePwdDto updatePwdDto){
        return userService.updatePassword(updatePwdDto);
    }




}
